package com.liangzhong.proxy.explore
{
	import com.liangzhong.commandconst.commandType.MainCommandType;
	import com.liangzhong.commandconst.explore.SMDExplore;
	import com.liangzhong.commandconst.logic.CMD_Logic;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.explore.ExploreDataManager;
	import com.liangzhong.model.type.BuildingType;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.liangzhong.proxyconstant.explore.ExploreProxyConstant;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	
	import lz.controls.Alert;

	/**
	 * 探索 
	 * @author Administrator
	 * 
	 */	
	public class ExploreProxy extends BaseProxy
	{
		public static const PROXYNAME:String = "ExploreProxy_proxyName";
		private var _exploreDataMangaer:ExploreDataManager;
		public function ExploreProxy(proxyName:String=PROXYNAME, data:Object=null)
		{
			super(proxyName, data);
			_exploreDataMangaer = AppData.getInstance().exploreDataManager;
		}
		
		/**接受 
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */		
		override public function onSocketRead(mainCmdID:uint, subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case MainCommandType.MAINDEAL:
					if(subCmdID == SMDExplore.UP_EXPLORE)
					{
						getCourtUp(buffer);
						return true;
					}
					break;
				case MainCommandType.PUBLICCOMMANDTYPE:
					return onDeal(subCmdID,buffer,dataSize);
			}
			return false;
		}
		/**
		 *处理 
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @return 
		 * 
		 */		
		private function onDeal(subCmdID:uint,
								buffer:ByteArray, 
								dataSize:int):Boolean
		{
			switch(subCmdID)
			{
				//					case SMDCourt.COURT_UP:
				//						return true;
//				case SMDExplore.UP_EXPLORE:
//					getCourtUp(buffer);
//					return true;
				case SMDExplore.BUYEXPLORETIME:
					getBuyExploretime(buffer);
					return true;
				case SMDExplore.ADDFEEL:
					getAddFeel(buffer);
					return true;
				case SMDExplore.SING:
					getSign(buffer);
					return true;
				case SMDExplore.EXPLORE_PLAYER:
					getExplorePlayer(buffer);
					return true;
					
			}
			return false;
		}
		/**
		 *请求探索信息  
		 * 
		 */
		public function requestExploreInfo():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.EXPLORE_INFO;
			ProxyUtils.instance.webClient(getExploreInfo,variables,false);
		}
		/**
		 *返回探索信息 
		 * @param $data
		 * 
		 */
		private function getExploreInfo($data:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($data,ExploreProxy);
			if(obj.retcode == 0)
			{
				_exploreDataMangaer.analysisExplore(obj.exploreinfo);
				this.sendNotification(ExploreProxyConstant.GET_EXPLORE_INFO);
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		
		/**
		 *请求升级  
		 * 
		 */
		public function requestCourtUp():void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,MainCommandType.MAINDEAL,SMDExplore.UP_EXPLORE);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回球场升级 
		 * 
		 */
		private function getCourtUp(buffer:ByteArray):void
		{
			
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				
				_exploreDataMangaer.exploreInfo.needlevel = buffer.readShort();
				
				_exploreDataMangaer.exploreInfo.level = buffer.readByte();
				_exploreDataMangaer.exploreInfo.coin = buffer.readInt();
				_exploreDataMangaer.exploreInfo.fame = buffer.readInt();
				_exploreDataMangaer.exploreInfo.explorecount = buffer.readByte();
				_exploreDataMangaer.exploreInfo.exploreplayer = buffer.readByte();
				var curtime :Number = AppData.getInstance().time;
				_exploreDataMangaer.exploreInfo.pretime = curtime;
				this.sendNotification(ExploreProxyConstant.GET_UP_EXPLORE,{status:true});
				Alert.showErrorTip("升级成功");
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				this.sendNotification(ExploreProxyConstant.GET_UP_EXPLORE,{status:false});
			}
		}
		/**
		 *请求冷却时间 
		 * @param $obj
		 * 
		 */
		public function requestClearUpTime($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 1,MainCommandType.MAINDEAL,CMD_Logic.DROP_BUILDING_Time);
			sendBuf.writeByte(BuildingType.EXPLORE);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		
		/**
		 *购买探索次数 
		 * 
		 */
		public function requestBuyExploreTime($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,MainCommandType.PUBLICCOMMANDTYPE,SMDExplore.BUYEXPLORETIME);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回购买次数 
		 * @param buffer
		 * 
		 */
		private function getBuyExploretime(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				var time:int = buffer.readInt();
				_exploreDataMangaer.exploreInfo.explorecoin = time;
				this.sendNotification(ExploreProxyConstant.GET_BUY_EXPLORE_TIME,{status:true});
				Alert.showErrorTip("购买成功");
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				this.sendNotification(ExploreProxyConstant.GET_BUY_EXPLORE_TIME,{status:false});
			}
		}
		/**
		 *请求增加好感度 
		 * @param $obj
		 * 
		 */
		public function requestAddFeel($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 5,MainCommandType.PUBLICCOMMANDTYPE,SMDExplore.ADDFEEL);
			sendBuf.writeByte($obj.tyep);
			sendBuf.writeInt($obj.id);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回增加好感度 
		 * @param buffer
		 * 
		 */
		private function getAddFeel(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				Alert.showErrorTip("成功增加好感度");
				var id:int = buffer.readInt();
				var feel:int = buffer.readByte();
				_exploreDataMangaer.exploreInfo.changeFeel(id,feel);
				this.sendNotification(ExploreProxyConstant.GET_ADD_FEEL,{status:true});
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				this.sendNotification(ExploreProxyConstant.GET_ADD_FEEL,{status:false});
			}
		}
		/**
		 *请求签约 
		 * @param $obj
		 * 
		 */
		public function requestSign($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 5,MainCommandType.PUBLICCOMMANDTYPE,SMDExplore.SING);
			sendBuf.writeByte($obj.type);
			sendBuf.writeInt($obj.id);
//			trace("请求签约ID"+ $obj.id);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
			
		/**
		 *返回签约  
		 * @param buffer
		 * 
		 */
		private function getSign(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				Alert.showErrorTip("签约成功，请到仓库查看！");
				var id:int = buffer.readInt();
//				trace("返回签约ID"+ id);
				_exploreDataMangaer.exploreInfo.signStar(id);
				this.sendNotification(ExploreProxyConstant.GET_SIGN,{status:true,id:id});
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				this.sendNotification(ExploreProxyConstant.GET_SIGN,{status:false});
			}
		}
		
		private var tempTime:int;
		/**
		 *探索球员 
		 * 
		 */
		public function requestExplorePlayer($obj:Object):void
		{
//			tempTime = getTimer();
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 2,MainCommandType.PUBLICCOMMANDTYPE,SMDExplore.EXPLORE_PLAYER);
			sendBuf.writeByte($obj.land);
			sendBuf.writeByte($obj.type);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
			
		/**
		 *返回探索球员 
		 * 
		 */
		private function getExplorePlayer(buffer:ByteArray):void
		{
			
//			trace("探索时间--》" + (getTimer() - tempTime));
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
//				var id:int = buffer.readInt();
//				_exploreDataMangaer.exploreInfo.signStar(id);
				_exploreDataMangaer.exploreInfo.exploretime = AppData.getInstance().time;
				_exploreDataMangaer.exploreInfo.explorePlayer(buffer);
				this.sendNotification(ExploreProxyConstant.GET_EXPLORE_PLAYER,{status:true});
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				this.sendNotification(ExploreProxyConstant.GET_EXPLORE_PLAYER,{status:false});
			}
		}
	}
}